package test.com.dcivision.framework;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class DatabaseManage {
  public static int DATABASE_TYPE_MYSQL     = 0;

  public static int DATABASE_TYPE_SQLSERVER = 1;

  public static int DATABASE_TYPE_ORACLE    = 2;


  private static Connection  getConnection(int dbType) throws Exception {

     Connection connection;
    String URL = null;
    String user = null;
    String password = null;
    java.util.Properties properties=new Properties();
    if (dbType == DATABASE_TYPE_MYSQL) {
      URL = "jdbc:mysql://localhost:3306/eip317update?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true";
      user = "root";
      password = "";
      Class.forName("com.mysql.jdbc.Driver");
      properties.setProperty("username", "root");
      properties.setProperty("password", "");
      properties.setProperty("autocommit", "false");
      properties.setProperty("maxActive", "60");
      properties.setProperty("defaultAutoCommit", "false");
      /*
       * URL = "jdbc:mysql://localhost:3306/eip317?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true"; user = "root"; password = ""; Class.forName("com.mysql.jdbc.Driver");
       */} else if (dbType == DATABASE_TYPE_SQLSERVER) {

      // *** Jtds connection ***
      //      URL = "jdbc:jtds:sqlserver://localhost:1433/eip317Dbunit";
      //      user = "sa";
      //      password = "";
      //      Class.forName("net.sourceforge.jtds.jdbc.Driver");

      /// *** Sqlserver Driver ***
      //
      URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=eip318Dbunit;SelectMethod=cursor;maxActive=60;maxIdle=3;defaultAutoCommit=false;defaultReadOnly=false";
      user = "sa";
      password = "";
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

      // *** Jdbc-odbc ***
      //      URL = "jdbc:odbc:eip318dbunit";
      //      user = "sa";
      //      password = "";
      //      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    } else if (dbType == DATABASE_TYPE_ORACLE) {
      URL = "jdbc:oracle:thin:@//203.189.168.35:1521/ParaDM";
      user = "sa";
      password = "";
      Class.forName("oracle.jdbc.OracleDriver");
    } else {
      throw new Exception("Unknow Database Type");
    }

    connection = DriverManager.getConnection(URL,user,password);
    connection.setAutoCommit(true);
    return connection;
  }
  
  public static Connection getCurrentConnection() {
    try {
      return getConnection(DATABASE_TYPE_MYSQL);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}
